﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Bids.Core.Common.Data
{
    public static class StringExtend
    {
        /// <summary>
        /// 截断字符串
        /// </summary>
        /// <param name="sourceStr">源字符串</param>
        /// <param name="length">要截取的长度</param>
        /// <param name="moreStr">更多显示的符号</param>
        /// <returns></returns>
        public static string CutStr(this string sourceStr, int length, string moreStr)
        {
            if (!string.IsNullOrEmpty(sourceStr))
            {
                if (sourceStr.Length > length)
                {
                    if(string.IsNullOrEmpty(moreStr))
                        moreStr = "...";

                    sourceStr = sourceStr.Substring(0, length) + moreStr;
                }
            }
            return sourceStr;
        }
        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="content">要加密的明文</param>
        /// <param name="key">密匙</param>
        /// <returns></returns>
        public static string DesEncrypt(string content, string key)
        {
            string result = string.Empty;
            if (content != string.Empty)
            {
                try
                {
                    key = PrepareKey(key);
                    byte[] data = Encoding.UTF8.GetBytes(content);
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(key);
                    ICryptoTransform desencrypt = des.CreateEncryptor();
                    byte[] resultBype = desencrypt.TransformFinalBlock(data, 0, data.Length);
                    result = BitConverter.ToString(resultBype);
                }
                catch { }
            }
            return result;
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="content">要解密的密文</param>
        /// <param name="key">密匙</param>
        /// <returns></returns>
        public static string DesDecrypt(string content, string key)
        {
            string result = string.Empty;
            if (content != string.Empty)
            {
                try
                {
                    key = PrepareKey(key);
                    string[] sInput = content.Split("-".ToCharArray());
                    byte[] data = new byte[sInput.Length];
                    for (int i = 0; i < sInput.Length; i++)
                    {
                        data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
                    }
                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                    des.Key = ASCIIEncoding.ASCII.GetBytes(key);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(key);
                    ICryptoTransform desencrypt = des.CreateDecryptor();
                    byte[] resultBype = desencrypt.TransformFinalBlock(data, 0, data.Length);
                    result = Encoding.UTF8.GetString(resultBype);
                }
                catch { }
            }
            return result;
        }

        /// <summary>
        /// 返回错位MD5密匙
        /// </summary>
        /// <param name="key">密匙明文</param>
        /// <returns></returns>
        private static string PrepareKey(string key)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key, "MD5").Substring(12, 8);
        }
    }
}
